package cn.wolfcode.web.controller;


import cn.wolfcode.Enum.LevelEnum;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiredPermission(name="公告页面",expermission="notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("enums", LevelEnum.values());
        model.addAttribute("pageInfo", pageInfo);
        return "notice/list";
    }


    @RequiredPermission(name="公告删除",expermission="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expermission="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expermission="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));
        }
        return "notice/input";
    }
    @RequestMapping("/show")
    @RequiredPermission(name = "查看公告",expermission = "notice:show")
    public String show(Long id,Model model){
        if (id!=null){
            model.addAttribute("notice",noticeService.get(id));
        }
        return "notice/show";
    }

    @ResponseBody
    @RequestMapping("/publish")
    @RequiredPermission(name = "发布状态",expermission = "notice:publish")
    public JsonResult publish(Long id,Boolean status){
            noticeService.publish(id,status);
            return new JsonResult();
    }
}
